package com.atguigu.common.valid;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author ShiJieLin
 * @Date 2022/4/2 22:41
 * @Version 1.0
 */
@Slf4j
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    Set<Integer> set = new HashSet<>();

    /** 获取使用该注解时，填写的值
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {

        for (int value : constraintAnnotation.values()) {
            set.add(value);
        }
    }

    /**
     * @param value 前台传过来，将要被校验的值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        //判断是否有包含的值
        boolean contains = set.contains(value);

        System.out.println(context + "***************************");

        return contains;
    }
}
